<template>
  <div class="app-container">
    <el-dialog :title="title" :visible.sync="addUpdateOpen" width="770px" :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="addUpdateOpenRules"  label-width="80px">
        <el-form-item label="参数名称" prop="parameterName">
          <el-input :hide-required-asterisk="true" autocomplete="off" v-model="form.parameterName" placeholder="请输入参数名称" maxlength="100" />
        </el-form-item>

        <el-form-item label="参数编码" prop="parameterCode">
          <el-input :hide-required-asterisk="true"  autocomplete="off" v-model="form.parameterCode" placeholder="请输入参数编码" maxlength="50" />
        </el-form-item>

        <el-form-item label="参数取值" prop="parameterValue">
          <el-input :hide-required-asterisk="true" autocomplete="off" v-model="form.parameterValue" placeholder="请输入参数取值" maxlength="100" />
        </el-form-item>

        <el-form-item label="参数描述" prop="description">
          <el-input :hide-required-asterisk="true" autocomplete="off" v-model="form.description" placeholder="请输入参数描述" maxlength="255" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button  size="medium" type="primary" @click="submitForm">确 定</el-button>
        <el-button  size="medium" @click="cancelAddUpdateOpen">取 消</el-button>
      </div>
    </el-dialog>

    <el-row :gutter="20">
      <el-col>
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="80px" v-show="showSearch" >
          <el-form-item label="参数名称" prop="parameterName">
            <el-input v-model="queryParams.parameterName" placeholder="请输入参数名称" size="small" clearable style="width: 150px"/>
          </el-form-item>
          <el-form-item label="参数编码" prop="parameterCode">
            <el-input v-model="queryParams.parameterCode" placeholder="请输入参数编码" size="small" clearable style="width: 150px"/>
          </el-form-item>
          <el-form-item label="参数取值" prop="parameterValue">
            <el-input v-model="queryParams.parameterValue" placeholder="请输入参数取值" size="small" clearable style="width: 150px"/>
          </el-form-item>
          <el-form-item label="参数描述" prop="description">
            <el-input v-model="queryParams.description" placeholder="请输入参数描述" size="small" clearable style="width: 150px"/>
          </el-form-item>
          <el-form-item label="是否内置" prop="builtIn">
            <el-select  @change="handleQuery"  v-model="queryParams.builtIn" style="width: 150px" size="small" clearable placeholder="请选择">
              <el-option key="" label="全部" value=""/>
              <el-option
                v-for="item in builtInCodeList"
                :key="item.dictCode"
                :label="item.dictName"
                :value="item.dictCode"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary"  size="mini" icon="el-icon-search"  @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh"  size="mini"  @click="resetQuery('queryForm')">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" v-hasPermi="['saveSystemParameter']"  @click="handleAdd">新增系统参数</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="primary"  v-hasPermi="['resetSystemParameterCache']"  icon="el-icon-refresh" size="mini" @click="resetSystemParameterCache">刷新缓存</el-button>
          </el-col>

          <right-toolbar :showSearch.sync="showSearch" @queryTable="querySystemParameterPageResList" :columns="columns"></right-toolbar>
        </el-row>
        <el-table :stripe="true" v-loading="loading" :data="dataList" style="width: 100%" @sort-change='tableSortChange'>
          <el-table-column label="参数名称" align="left" prop="parameterName"  width="300"/>
          <el-table-column label="参数编码" align="left" prop="parameterCode"  width="200"/>
          <el-table-column label="参数取值" align="left" prop="parameterValue"  width="200"/>
          <el-table-column label="参数描述" align="left" prop="description"/>
          <el-table-column label="是否内置" align="left" prop="builtInName" width="150">
            <template slot-scope="scope">
              <span :style="'color:'+scope.row.builtInColor">{{scope.row.builtInName}}</span>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="left" prop="createTime" sortable='custom' width="200" />
          <el-table-column label="修改时间" align="center" prop="updateTime" sortable='custom' width="200" />
          <el-table-column label="操作"  align="center" width="150" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" v-hasPermi="['updateSystemParameter']"  @click="handleUpdate(scope.row)">修改</el-button>
              <el-button size="mini" v-if="scope.row.builtIn==='NO'" type="text" icon="el-icon-delete" v-hasPermi="['deleteSystemParameter']"  @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total>0"  :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.rows" @pagination="querySystemParameterPageResList"/>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import {querySystemParameterPageResList, saveSystemParameter, updateSystemParameter,deleteSystemParameterByParameterId, resetSystemParameterCache } from '@/api/system/systemParameter'
  import { getDicListByDictCodeList } from '@/api/common/dict'
  //默认查询参数
  let defaultQueryParams = {
    parameterName: undefined,
    parameterCode: undefined,
    parameterValue: undefined,
    description: undefined,
    builtIn: "",
    page: 1,
    rows: 10
  }
  export default {
    name: 'systemParameterAdmin',
    data() {
      return {
        // 遮罩层
        loading: true,
        // 总条数
        total: 0,
        // 显示搜索条件
        showSearch: true,
        //操作日志
        dataList: [],
        // 增加修改表单参数
        form: {},
        // 弹出层标题
        title: "",
        // 是否显示新增修改弹出层
        addUpdateOpen: false,
        // 新增修改表单校验
        addUpdateOpenRules: {
          parameterName: [
            { required: true, message: "参数名称不能为空",trigger: "blur" }
          ],
          parameterCode: [
            { required: true, message: "参数编码不能为空",trigger: "blur" },
            { validator: this.formValidate.enNOLine,trigger: "blur"}
          ],
          parameterValue: [
            { required: true, message: "参数取值不可为空",trigger: "blur" }
          ]
        },
        //是否内置
        builtInCodeList: [],
        // 查询参数
        queryParams: { ...defaultQueryParams },
        //列信息
        columns: []
      }
    },
    created() {
      this.querySystemParameterPageResList()
      this.initDict()
    },
    methods: {
      //排序
      tableSortChange(column) {
        //格式化排序参数，构建中
        this.orderByFormat(this.queryParams,column);
        //执行查询方法
        this.querySystemParameterPageResList()
      },
      /** 查询参数列表 */
      querySystemParameterPageResList() {
        this.loading = true
        querySystemParameterPageResList(this.queryParams).then(res => {
            this.dataList = res.data.records
            this.total = res.data.total
          }
        ).finally(() => {
          //关闭加载特效
          this.loading = false
        })
      },
      /** 新增按钮操作 */
      handleAdd(row) {
        this.reset();
        this.addUpdateOpen = true;
        this.title = "添加系统参数";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        //修改表单填充
        this.form = {...row};
        this.addUpdateOpen = true;
        this.title = "修改系统参数";
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$confirm('是否确认删除所选参数?', "警告", {
          cancelButtonText: "取消",
          confirmButtonText: "确定",
          type: "warning"
        }).then(function() {
          const parameterId={parameterId:row.parameterId}
          return deleteSystemParameterByParameterId(parameterId);
        }).then(response => {
          this.$modal.msgSuccess(response.message)
          this.querySystemParameterPageResList();
        }).catch(function() {});
      },
      /** 刷新系统参数缓存操作 */
      resetSystemParameterCache() {
        this.$confirm('是否确认刷新系统参数缓存?', "警告", {
          cancelButtonText: "取消",
          confirmButtonText: "确定",
          type: "warning"
        }).then(function() {
          return resetSystemParameterCache();
        }).then(response => {
          this.$modal.msgSuccess(response.message)
          this.querySystemParameterPageResList();
        }).catch(function() {});
      },

      /** 提交保存或修改按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.parameterId !== undefined) {
              updateSystemParameter(this.form).then(response => {
                this.$modal.msgSuccess(response.message)
                this.querySystemParameterPageResList();
                this.addUpdateOpen = false
              });
            } else {
              saveSystemParameter(this.form).then(response => {
                this.$modal.msgSuccess(response.message)
                this.querySystemParameterPageResList();
                this.addUpdateOpen = false
              });
            }
          }
        });
      },

      /** 重置按钮操作 */
      resetQuery(fromId) {
        this.resetForm(fromId)
        this.handleQuery()
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1
        this.querySystemParameterPageResList()
      },
      //获取数据字典
      initDict(){
        let dictArr = ["YES_NO"];
        getDicListByDictCodeList(dictArr).then(res => {
          this.builtInCodeList = JSON.parse(JSON.stringify(res.data['YES_NO']));
        });
      },
      // 取消修改增加弹出层按钮
      cancelAddUpdateOpen() {
        this.addUpdateOpen = false
      },
      // 表单重置
      reset() {
        this.form = {
          parameterId: undefined,
          parameterName: undefined,
          parameterCode: undefined,
          parameterValue: undefined,
          description: undefined,
          builtIn: undefined
        };
        this.resetForm('form')
      }
    }
  }
</script>
